package SQL

import org.apache.spark.sql
import org.junit.Test

class 无类型转换的算子 {
  val spark = new sql.SparkSession.Builder()
    .master("local[6]")
    .appName("DataSet")
    .getOrCreate()
  //导入隐式转换
  import spark.implicits._

  @Test
  def select(): Unit = {
    val ds = spark.createDataset(Seq(person("zs", 15), person("ww", 18), person("ww", 15)))
    ds.select('name).show()
    ds.selectExpr("sum(age) as sum_age").show()
    import org.apache.spark.sql.functions._
    ds.select(expr("sum(age)")).show()
  }
  @Test
  def createcol(): Unit ={
    import org.apache.spark.sql.functions._
    val ds = spark.createDataset(Seq(person("zs", 15), person("ww", 18), person("ww", 15)))
    //新建列
    ds.withColumn("random",expr("rand()")).show()
    //依照name的数据增加一列
    ds.withColumn("name_new",'name).show()
    //重命名
    ds.withColumnRenamed("name","new_name").show()
  }
  @Test
  def groupBy(): Unit ={
    import org.apache.spark.sql.functions._
    val ds = spark.createDataset(Seq(person("zs", 15), person("ww", 18), person("ww", 15)))
    ds.groupBy('name).agg(mean('age)).show()
  }
}
